<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('inventory:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="编号">
                <a-input v-model="queryParam.code" allow-clear placeholder="请输入编号"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select style="width: 100%" allow-clear v-model="queryParam.status" placeholder="请选择盘库状态">
                  <a-select-option v-for="(item,index) in inventoryStatusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" >
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('inventory:add')" >
          <a-button type="primary" v-if="hasPerm('inventory:add')" icon="plus" @click="goToAddForm()">新增盘库</a-button>
        </template>
        <span slot="statusScopedSlots" slot-scope="text">
          {{ 'inventory_status' | dictType(text) }}
        </span>
        <span slot="typeScopedSlots" slot-scope="text">
          {{ 'inventory_type' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('inventory:edit')" @click="goToView(record)">详情</a>
          <a-divider type="vertical" v-if="hasPerm('inventory:edit') & record.status === 'CONFIRM'"/>
          <a-popconfirm v-if="hasPerm('inventory:edit') & record.status === 'CONFIRM'" placement="topRight" title="确认盘库？" @confirm="() => goToConfirm(record)">
            <a>确认</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('inventory:edit') & record.status === 'PROCESSED'"/>
          <a-popconfirm v-if="hasPerm('inventory:edit') & record.status === 'PROCESSED'" placement="topRight" title="确认复盘？" @confirm="() => goToRepeat(record)">
            <a>复盘</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('inventory:edit') & record.status !== 'CONFIRM' & record.status !== 'FINISHED'"/>
          <a-popconfirm v-if="hasPerm('inventory:edit') & record.status !== 'CONFIRM' & record.status !== 'FINISHED'" placement="topRight" title="确认关闭？" @confirm="() => goToFinished(record)">
            <a>关闭</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('inventory:edit') & record.status === 'CONFIRM'"/>
          <a v-if="hasPerm('inventory:edit') & record.status === 'CONFIRM'" @click="goToEditForm(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('inventory:delete') & record.status === 'CONFIRM'"/>
          <a-popconfirm v-if="hasPerm('inventory:delete') & record.status === 'CONFIRM'" placement="topRight" title="确认删除？" @confirm="() => inventoryDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import moment from 'moment'
  import { inventoryPage, inventoryDelete, inventoryConfirm, inventoryRepeat, inventoryFinished } from '@/api/modular/main/stock/inventory/inventoryManage'
  export default {
    components: {
      STable
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        inventoryStatusData: [],
        inventoryTypeData: [],
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '编号',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'code'
          },
          {
            title: '类型',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'type',
            scopedSlots: { customRender: 'typeScopedSlots' }
          },
          {
            title: '状态',
            align: 'center',
            width: 120,
            ellipsis: true,
            dataIndex: 'status',
            scopedSlots: { customRender: 'statusScopedSlots' }
          },
          {
            title: '确认人',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'confirmBy'
          },
          {
            title: '确认时间',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'confirmTime'
          },
          {
            title: '复盘操作人',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'repeatBy'
          },
          {
            title: '复盘操作时间',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'repeatTime'
          },
          {
            title: '关闭人',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'closeBy'
          },
          {
            title: '关闭时间',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'closeTime'
          },
          {
            title: '备注',
            align: 'center',
            width: 250,
            ellipsis: true,
            dataIndex: 'remark'
          },
          {
            title: '更新人',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'updateUserName'
          },
          {
            title: '更新时间',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'updateTime'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return inventoryPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {
      if (this.hasPerm('inventory:edit') || this.hasPerm('inventory:delete')) {
        this.columns.push({
          title: '操作',
          align: 'center',
          width: 200,
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          fixed: 'right'
        })
      }
      const statusOption = this.$options
      this.inventoryStatusData = statusOption.filters['dictData']('inventory_status')
      const inventoryTypeOption = this.$options
      this.inventoryTypeData = inventoryTypeOption.filters['dictData']('inventory_type')
    },
    activated () {
      this.$refs.table.refresh()
    },
    methods: {
      moment,
      inventoryDelete (record) {
        inventoryDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败:' + res.message)
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      goToView (record) {
        this.$router.push({
          path: '/inventoryForm/view',
          query: {
            code: record.code
          }
        })
      },
      goToConfirm (record) {
        inventoryConfirm(record).then((res) => {
          if (res.success) {
            this.$message.success('确认成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('确认失败:' + res.message)
          }
        })
      },
      goToRepeat (record) {
        inventoryRepeat(record).then((res) => {
          if (res.success) {
            this.$message.success('复盘操作成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('复盘操作失败:' + res.message)
          }
        })
      },
      goToFinished (record) {
        inventoryFinished(record).then((res) => {
          if (res.success) {
            this.$message.success('盘库任务关闭成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('操作失败:' + res.message)
          }
        })
      },
      goToAddForm () {
        this.$router.push({ path: '/inventoryForm/add' })
      },
      goToEditForm (record) {
        this.$router.push({
          path: '/inventoryForm/edit',
          query: {
            code: record.code
          }
        })
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
